<?php
/* 

example usage: 

include('Smarty.class.php'); 
include('mysql_cache_handler.php'); 

$smarty = new Smarty; 
$smarty->cache_handler_func = 'mysql_cache_handler'; 

$smarty->display('index.tpl'); 


mysql database is expected in this format: 
     
create database SMARTY_CACHE; 

create table CACHE_PAGES( 
CacheID char(32) PRIMARY KEY, 
CacheContents MEDIUMTEXT NOT NULL 
); 

*/ 

function mysql_cache_handler($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null, $exp_time=null) 
{ 
    // set db host, user and pass here 
    $db_host = 'localhost'; 
    $db_user = 'ddt'; 
    $db_pass = 'ddt123456'; 
    $db_name = 'ddt'; 
    $use_gzip = true; 
     
    // create unique cache id 
    $CacheID = md5($tpl_file.$cache_id.$compile_id); 
     
    if(! $link = mysql_pconnect($db_host, $db_user, $db_pass)) { 
        $smarty_obj->_trigger_error_msg("cache_handler: could not connect to database"); 
        return false; 
    } 
    mysql_select_db($db_name); 
    mysql_query("set names utf8");

    switch ($action) { 
        case 'read': 
            // read cache from database 
            $results = mysql_query("select CacheContents from CACHE_PAGES where CacheID='$CacheID'"); 
            if(!$results) { 
                $smarty_obj->_trigger_error_msg("cache_handler: query failed.");             
            } 
            $row = mysql_fetch_array($results,MYSQL_ASSOC); 
             
            if($use_gzip && function_exists("gzuncompress")) { 
                $cache_content = gzuncompress($row["CacheContents"]); 
            } else { 
                $cache_content = $row["CacheContents"]; 
            } 
            $return = $results; 
            break; 
        case 'write': 
            // save cache to database 
             
            if($use_gzip && function_exists("gzcompress")) { 
                // compress the contents for storage efficiency 
                $contents = gzcompress($cache_content); 
            } else { 
                $contents = $cache_content; 
            } 
            echo "$contents";
            $results = mysql_query("replace into CACHE_PAGES values( 
                            '$CacheID', 
                            '".addslashes($contents)."') 
                        "); 
            if(!$results) { 
                $smarty_obj->_trigger_error_msg("cache_handler: query failed.");             
            } 
            $return = $results; 
            break; 
        case 'clear': 
            // clear cache info 
            if(empty($cache_id) && empty($compile_id) && empty($tpl_file)) { 
                // clear them all 
                $results = mysql_query("delete from CACHE_PAGES");             
            } else { 
                $results = mysql_query("delete from CACHE_PAGES where CacheID='$CacheID'");             
            } 
            if(!$results) { 
                $smarty_obj->_trigger_error_msg("cache_handler: query failed.");             
            } 
            $return = $results; 
            break; 
        default: 
            // error, unknown action 
            $smarty_obj->_trigger_error_msg("cache_handler: unknown action \"$action\""); 
            $return = false; 
            break; 
    } 
    mysql_close($link); 
    return $return; 
     
} 

function memcache_cache_handler($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null, $exp_time=null) 
{ 
    // set db host, user and pass here 
    $mem_host = '127.0.0.1'; 
    $mem_port = 11211; 
    
    $use_gzip = false; 
     
    // create unique cache id 
    $CacheID = md5($tpl_file.$cache_id.$compile_id); 
     
    $memcache=new Memcache();
    
    if(!$memcache->connect($mem_host,$mem_port)) { 
        $smarty_obj->_trigger_error_msg("cache_handler: could not connect to memcache"); 
        return false; 
    } 
    

    switch ($action) { 
        case 'read': 
            // read cache from database 
            $results = $memcache->get($CacheID); 
            //if(!$results) { 
               // $smarty_obj->_trigger_error_msg("cache_handler: query failed.");             
           // } 
            $row = $results; 
             
            if($use_gzip && function_exists("gzuncompress")) { 
                $cache_content = gzuncompress($row); 
            } else { 
                $cache_content = $row; 
            } 
            $return = $results; 
            break; 
        case 'write': 
            // save cache to database 
             
            if($use_gzip && function_exists("gzcompress")) { 
                // compress the contents for storage efficiency 
                $contents = gzcompress($cache_content); 
            } else { 
                $contents = $cache_content; 
            } 
            $results =$memcache->set($CacheID,$contents);
            
            if(!$results) { 
                $smarty_obj->_trigger_error_msg("cache_handler: query failed.");             
            } 
            $return = $results; 
            break; 
        case 'clear': 
            // clear cache info 
            if(empty($cache_id) && empty($compile_id) && empty($tpl_file)) { 
                // clear them all 
                $results = $memcache->flush();             
            } else { 
                $results = $memcache->delete($CacheID);             
            } 
            if(!$results) { 
                $smarty_obj->_trigger_error_msg("cache_handler: query failed.");             
            } 
            $return = $results; 
            break; 
        default: 
            // error, unknown action 
            $smarty_obj->_trigger_error_msg("cache_handler: unknown action \"$action\""); 
            $return = false; 
            break; 
    } 
    $memcache->close();
    return $return; 
     
} 


function apc_cache_handler($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null, $exp_time=null) 
{ 
   
    
    $use_gzip = false; 
     
    // create unique cache id 
    $CacheID = md5($tpl_file.$cache_id.$compile_id); 
     
    
    
    
    

    switch ($action) { 
        case 'read': 
            // read cache from database 
            $results = apc_fetch($CacheID); 
            //if(!$results) { 
               // $smarty_obj->_trigger_error_msg("cache_handler: query failed.");             
           // } 
            $row = $results; 
             
            if($use_gzip && function_exists("gzuncompress")) { 
                $cache_content = gzuncompress($row); 
            } else { 
                $cache_content = $row; 
            } 
            $return = $results; 
            break; 
        case 'write': 
            // save cache to database 
             
            if($use_gzip && function_exists("gzcompress")) { 
                // compress the contents for storage efficiency 
                $contents = gzcompress($cache_content); 
            } else { 
                $contents = $cache_content; 
            } 
            $results =apc_store($CacheID,$contents);
            
            if(!$results) { 
                $smarty_obj->_trigger_error_msg("cache_handler: query failed.");             
            } 
            $return = $results; 
            break; 
        case 'clear': 
            // clear cache info 
            if(empty($cache_id) && empty($compile_id) && empty($tpl_file)) { 
                // clear them all 
                $results = apc_clear_cache("user");             
            } else { 
                $results = apc_delete($CacheID);             
            } 
            if(!$results) { 
                $smarty_obj->_trigger_error_msg("cache_handler: query failed.");             
            } 
            $return = $results; 
            break; 
        default: 
            // error, unknown action 
            $smarty_obj->_trigger_error_msg("cache_handler: unknown action \"$action\""); 
            $return = false; 
            break; 
    } 
    
    return $return; 
     
} 
?>